//
//  KATNavTransition.m
//  KATFramework
//
//  Created by Kantice on 2017/8/2.
//  Copyright © 2017年 KatApp. All rights reserved.
//

#import "KATNavTransition.h"
#import "KATMacros.h"





@implementation KATNavTransition


//获取动画实例(默认为出现动画)
+ (instancetype)transition
{
    KATNavTransition *transition=[[[self alloc] init] autorelease];
    
    transition.duration=0.4;
    transition.isDismissAnimation=NO;
    transition.isVerticalAnimation=NO;
    
    return transition;
}



#pragma -mark UIViewControllerAnimatedTransitioning

//实现present动画逻辑代码(因为路由器的跳转机制，这里不管是出现还是消失，都是出现动画)
- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
    //生成fromView和toView
    
    //通过viewControllerForKey取出转场前后的控制器
    UIView *toView=[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    toView.frame=[UIScreen mainScreen].bounds;

    //通过viewControllerForKey取出转场前后的控制器
    UIView *fromView=[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
    fromView.frame=[UIScreen mainScreen].bounds;
    
    //消失的View(截图)
    UIView *dismissView=[fromView snapshotViewAfterScreenUpdates:NO];
    dismissView.frame=[UIScreen mainScreen].bounds;
    
    
    //如果要对视图做转场动画，视图就必须要加入containerView中才能进行，可以理解containerView管理着所有做转场动画的视图
    UIView *containerView=[transitionContext containerView];
    
    if(!_isDismissAnimation)//出现动画
    {
        //将截图视图和toVC的view都加入ContainerView中
        [containerView addSubview:dismissView];
        [containerView addSubview:toView];
        
        //阴影
        UIView *shadow=[[[UIView alloc] init] autorelease];
        shadow.backgroundColor=KCOLOR_SHADOW_LIGHT;
        shadow.frame=dismissView.bounds;
        [dismissView addSubview:shadow];
        
        if(_isVerticalAnimation)//纵向
        {
            //动画
            dismissView.frame=CGRectMake(0, 0, fromView.bounds.size.width, fromView.bounds.size.height);
            toView.frame=CGRectMake(0, toView.bounds.size.height, toView.bounds.size.width, toView.bounds.size.height);
            shadow.alpha=0.0;
        }
        else//横向
        {
            //动画
            dismissView.frame=CGRectMake(0, 0, fromView.bounds.size.width, fromView.bounds.size.height);
            toView.frame=CGRectMake(toView.bounds.size.width, toView.frame.origin.y, toView.bounds.size.width, toView.bounds.size.height);
            shadow.alpha=0.0;
        }
        
        [UIView animateWithDuration:_duration animations:^
        {
            if(!_isVerticalAnimation)//横向
            {
                dismissView.frame=CGRectMake(-MIN(SCREEN_WIDTH, SCREEN_HEIGHT)*0.2, 0, fromView.bounds.size.width, fromView.bounds.size.height);
            }
            
            toView.frame=toView.bounds;
            shadow.alpha=1.0;
        }
        completion:^(BOOL finished)
        {
            //移除截图
            [dismissView removeFromSuperview];
            
            //使用如下代码标记整个转场过程是否正常完成[transitionContext transitionWasCancelled]代表手势是否取消了，如果取消了就传NO表示转场失败，反之亦然，如果不用手势present的话直接传YES也是可以的，但是无论如何我们都必须标记转场的状态，系统才知道处理转场后的操作，否则认为一直还在转场中，会出现无法交互的情况！
            [transitionContext completeTransition:YES];
        }];
    }
    else//消失动画
    {
        //将截图视图和toVC的view都加入ContainerView中
        [containerView addSubview:toView];
        [containerView addSubview:dismissView];
        
        //阴影
        UIView *shadow=[[[UIView alloc] init] autorelease];
        shadow.backgroundColor=KCOLOR_SHADOW_LIGHT;
        shadow.frame=dismissView.bounds;
        [dismissView addSubview:shadow];
        
        if(!_isVerticalAnimation)//横向
        {
            toView.frame=CGRectMake(-MIN(SCREEN_WIDTH, SCREEN_HEIGHT)*0.2, 0, fromView.bounds.size.width, fromView.bounds.size.height);
        }
        
        //动画
        shadow.alpha=0.0;
        
        [UIView animateWithDuration:_duration animations:^
        {
            if(_isVerticalAnimation)//纵向
            {
                dismissView.frame=CGRectMake(0, dismissView.bounds.size.height, dismissView.bounds.size.width, dismissView.bounds.size.height);
            }
            else//横向
            {
                dismissView.frame=CGRectMake(dismissView.bounds.size.width, dismissView.frame.origin.y, dismissView.bounds.size.width, dismissView.bounds.size.height);
            }
            
            toView.frame=CGRectMake(0, 0, fromView.bounds.size.width, fromView.bounds.size.height);
            
            shadow.alpha=1.0;
        }
        completion:^(BOOL finished)
        {
            //移除截图
            [dismissView removeFromSuperview];
            
            //使用如下代码标记整个转场过程是否正常完成[transitionContext transitionWasCancelled]代表手势是否取消了，如果取消了就传NO表示转场失败，反之亦然，如果不用手势present的话直接传YES也是可以的，但是无论如何我们都必须标记转场的状态，系统才知道处理转场后的操作，否则认为一直还在转场中，会出现无法交互的情况！
            [transitionContext completeTransition:YES];
        }];
    }
}


//实现dismiss动画逻辑代码(因为路由器的跳转机制，这里不管是出现还是消失，都是出现动画，此代码无法执行)
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
    //生成fromView和toView

    //通过viewControllerForKey取出转场前后的控制器
    UIView *toView=[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
    
    //通过viewControllerForKey取出转场前后的控制器
    UIView *fromView=[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
    
    //消失的View(截图)
    UIView *dismissView=[fromView snapshotViewAfterScreenUpdates:YES];
    dismissView.frame=[UIScreen mainScreen].bounds;
    
    //如果要对视图做转场动画，视图就必须要加入containerView中才能进行，可以理解containerView管理着所有做转场动画的视图
    UIView *containerView=[transitionContext containerView];
    
    //将截图视图和toVC的view都加入ContainerView中
    [containerView addSubview:toView];
    [containerView addSubview:dismissView];
    
    
    //动画
    [UIView animateWithDuration:_duration animations:^
    {
        dismissView.frame=CGRectMake(dismissView.bounds.size.width, dismissView.frame.origin.y, dismissView.bounds.size.width, dismissView.bounds.size.height);
    }
    completion:^(BOOL finished)
    {
        //移除截图
        [dismissView removeFromSuperview];
        
        //使用如下代码标记整个转场过程是否正常完成[transitionContext transitionWasCancelled]代表手势是否取消了，如果取消了就传NO表示转场失败，反之亦然，如果不用手势present的话直接传YES也是可以的，但是无论如何我们都必须标记转场的状态，系统才知道处理转场后的操作，否则认为一直还在转场中，会出现无法交互的情况！
        [transitionContext completeTransition:YES];
    }];
}


//动画时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return _duration;
}


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{    
    //因为路由器的跳转机制，这里不管是出现还是消失，都是出现动画
    [self presentAnimation:transitionContext];
    
//    if(_isDismissAnimation)
//    {
//        [self dismissAnimation:transitionContext];
//    }
//    else
//    {
//        [self presentAnimation:transitionContext];
//    }
}




#pragma -mark UIViewControllerAnimatedTransitioning


//出现动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    return self;
}


//消失动画
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return self;
}



//释放内存
- (void)dealloc
{
    [super dealloc];
}


@end
